package com.annhoa.infrastructure.util;

import com.google.common.collect.Maps;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelImageUtils {

    public static Map<Integer, PictureData> importExcels(Workbook wb, Sheet sheet) {

        Map<Integer, PictureData> result = Maps.newHashMap();
        //读取sheet的图片放入Map
//        List<XSSFPictureData> pictures = (List<XSSFPictureData>) wb.getAllPictures();
//        if (pictures.size() != 0) {
//            for (XSSFShape shape : ((XSSFSheet) sheet).getDrawingPatriarch().getShapes()) {
//                XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
//                Map<String, PictureData> sheetIndexPicMap = new HashMap<>();
//                if (shape instanceof XSSFPicture) {
//                    XSSFPicture pic = (XSSFPicture) shape;
//                    XSSFPictureData picData = pic.getPictureData();
//                    //获取图片格式
//                    String ext = picData.suggestFileExtension();
//                    //保存的文件名
//                    String fileName = UUID.randomUUID().toString() + "." + ext;
//                    //获取坐标
//                    CTMarker ctMarker = anchor.getFrom();
//                    System.out.println(ctMarker.getRow() + ctMarker.getCol());
//                    sheetIndexPicMap.put(fileName, picData);
//                    result.add(sheetIndexPicMap);
//                }
//            }
//        }
        for (POIXMLDocumentPart dr : ((XSSFSheet) sheet).getRelations()) {
            if (dr instanceof XSSFDrawing) {
                Map<Integer, PictureData> sheetIndexPicMap = new HashMap<>();
                XSSFDrawing drawing = (XSSFDrawing) dr;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture pic = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = pic.getPreferredSize();
                    CTMarker ctMarker = anchor.getFrom();
                    // 取row 为key
                    String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
                    XSSFPictureData pictureData = pic.getPictureData();
                    result.put(ctMarker.getRow(), pictureData);
                }
            }
        }
        return result;
    }

    public static void printImg(Map<Integer, PictureData> map) throws IOException {

        Object key[] = map.keySet().toArray();
        for (int i = 0; i < map.size(); i++) {
            // 获取图片流
            PictureData pic = map.get(key[i]);
            // 获取图片索引
            String picName = key[i].toString() + "." + pic.suggestFileExtension();
            // 获取图片格式
            byte[] data = pic.getData();

            FileOutputStream out = new FileOutputStream("F:\\template\\" + picName);
            out.write(data);
            out.close();
        }
    }
}
